--[[
 -- *名称:DdzSuitAnalyzer
 -- *描述:斗地主分析器
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-07
 -- *修改日期:2017-11-07
 ]]

local DdzAnalysisResult = require(basePath.."suit/ddzanalysisresult");
local SortCard = require(basePath .. "suit/sortcard");
local DdzSuitAnalyzer = pokercore.class();

DdzSuitAnalyzer.LIMIT_SIZE = 20 ;
---
 -- [ctor 默认类的构造方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function DdzSuitAnalyzer:ctor(...)

end

function DdzSuitAnalyzer:getPriority()
   return 0 ;
end

---子类去实现
function DdzSuitAnalyzer:getSuitResult()
	local result = pokercore.new(DdzAnalysisResult);
  --print("getSuitResult->",result) ;
	result:setType(self:getSuitType());
	return result;
end

--[[
 -- [match 是否符合牌型条件，如果符合填充分析结果]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @param    term [description]
 -- @return    [description]
 ]]
function DdzSuitAnalyzer:match(cards,term)
	if not cards or cards:size() == 0 then
		return false ;
	end
   	local result = self:getSuitResult();
   	if not result then
   		return false ;
   	end
   	--填充分析结果
   	cards.result = result ;
   	if not self:checkRule(cards,term) then
       print("checkRule不成功,type->",self:getSuitType());
       return false ;
   	end
   	return true;
end

---
 -- [checkRule 检查规则]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @param    term [description]
 -- @return    [description]
 --
function DdzSuitAnalyzer:checkRule(cards,term)
    -- 排序 null，牌，癞子  点数从小到大排列牌Asc
    -- table.sort(cards:getCardList(),function(o1,o2) 
    --     if not o1 and not o2 then
    --         --print("--------------");
    --         return true ;
    --     else
    --       if not o1 then
    --          --print("--------------");
    --         return true ;
    --       elseif not o2 then
    --          --print("--------------");
    --         return false ;
    --       else
    --         --print("++++++")
    --         local result = o1:compareWithShapeTo(o2) ;
    --         --print("result->",result) ;
          
    --         if result > 0 then
    --            --print("return->",false) ;
    --           return false ;

    --         else
    --            --print("return->",true) ;
    --           return true ;
    --         end
    --       end
    --     end
    -- end) ;
    --self:sort(cards,term)
    PokerUtil.sortCards_asc(cards);
    --print("DdzSuitAnalyzer:checkRule**->",cards)
    -- 传进来的牌不应该有nil值
    if not cards:get(1) then
    	return false ;
    else
    	cards:getResult():collectNumber(cards,term);
    	if cards:getResult():getCardNum() > DdzSuitAnalyzer.LIMIT_SIZE  then
    		return false ;
    	else
        --print("DdzSuitAnalyzer:checkRule - > doCheckRule");
    		return self:doCheckRule(cards, term, cards:getResult());
    	end
    end 
end

---子类实现
function DdzSuitAnalyzer:doCheckRule(cards,term,result)

end


--[[
 -- [analysize 分析该牌列，在这个牌型下的大小权重]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @param    term [description]
 -- @return    [description]
 ]]
function DdzSuitAnalyzer:analysize(cards,term)
  --print("DdzSuitAnalyzer:analysize cards1",cards);
	local result = cards:getResult() ;
	if result:getLaziCardNum() ~= 0 then -- 有癞子，做癞子填充
    --print("DdzSuitAnalyzer:analysize cards->",cards)
    --print("DdzSuitAnalyzer:analysize has laizi!");
		local laiziCardList = cards:subList(result:getCardNum()-result:getLaziCardNum()+1,result:getCardNum());
    local cardStr = "";
    for _,laiZiCard in ipairs(laiziCardList) do
       cardStr = cardStr .. "(" .. tostring(laiZiCard) .. "),"
    end
    --print("DdzSuitAnalyzer:analysize has laiziCardList!",cardStr);
		for _,card in ipairs(laiziCardList) do
         	if card.isLaizi and card:getLaiziNumber() == 0 then
         		--print("some laiziCard's laziNumber is not valued !!!",debug.traceback());
         		self:doAnalysizeLaizi(cards,term,true);
         	end
		end
	end
  --print("DdzSuitAnalyzer:analysize cards len",#cards:getCardList());
	self:sort(cards,term);
  --print("DdzSuitAnalyzer:analysize cards",cards);
	--默认取第一张value 做weight评估
  result:setWeight(cards:get(1):getValue());
end


--[[
 -- [sort 牌列整理]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @param    term [description]
 -- @return    [description]
 ]]
function DdzSuitAnalyzer:sort(cards,term)
	cards:getResult():collectNumber(cards,term);
  local sortCardList = {} ;
  --利用SortCard来辅助排序
  for _,card in ipairs(cards:getCardList()) do
    table.insert(sortCardList,pokercore.new(SortCard,card,cards:getResult()));
  end
	--张数多的，花色点数小的在前面...
  --print("DdzSuitAnalyzer,sortCardList 1 len",#sortCardList);
  for k,v in ipairs(sortCardList) do
     --print(v);
  end
  table.sort(sortCardList,function(a,b)
     local result = a:compareTo(b) ;
     if result >= 0 then
         return false ;
     else
         return true ;
     end
  end);
  --print("DdzSuitAnalyzer,sortCardList 2 len",#sortCardList);
  --清空数据
  cards:clear();
  --重置数据
  for _,sortCard in ipairs(sortCardList) do
     cards:addCard(sortCard:getCard());
  end
end

---
 -- [analysizeLaizi  填充癞子，仅当牌中含有癞子，且不全是癞子的时候会进入这里]
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @param    term [description]
 -- @param    onlyMaxValue 仅赋值最大
 -- @return    [description]
 --
function DdzSuitAnalyzer:analysizeLaizi(cards,term,onlyMaxValue)
  if cards:getResult():isAllCardLazi() then
      local cardsResult = {} ;
      for _,card in pairs(cards:getCardList()) do
          card:changeCard(card:getNumber());
      end
      local cardsCopy = cards:clone();
      self:analysize(cardsCopy,term) ;
      table.insert(cardsResult,cardsCopy:clone())
      return  cardsResult;
  else
      return self:doAnalysizeLaizi(cards,term,onlyMaxValue);
  end
end

---
 -- [listPossibleCards description]
 -- 提牌算法，从手牌中分析出对应可能符合牌型的牌出来 <br>
 --*比如手牌 21QJ986554443 可以出的单牌就有 5 4 2 1 Q J 9 8 6 3 考虑拆牌
 -- @DateTime 2017-11-07
 -- @param    cards [description]
 -- @param    term [description]
 -- @param    onlyMaxValue [description]
 -- @return    [description]
 --
function DdzSuitAnalyzer:listPossibleCards(cards,term,onlyMaxValue)
    local result = {} ;
    --print("listPossibleCards 0")
    ----dump(cards,"DdzSuitAnalyzer:listPossibleCards cards");
    if cards and #cards:getCardList() ~= 0 then
       --排序,null,牌,癞子
       PokerUtil.sortCards_asc(cards);
       --print("listPossibleCards1")
       if cards:get(1) then
          --print("listPossibleCards!")
          local analysisresult = self:getSuitResult();
          if analysisresult then
              cards:setResult(analysisresult);
              analysisresult:collectNumber(cards, term);
              if self:containsPossibleCards(cards,term) then
                  table.merge(result,self:doListPossibleCards(cards, term, onlyMaxValue));
              end
          end
       end
       --print("listPossibleCards2")
    end
    --print("listPossibleCards end")
    return result;
end

---
 -- [doAnalysizeLaizi 牌组中含有癞子但不全是癞子的情况]
 -- 子类重写
 -- @DateTime 2017-11-08
 -- @param    cards [description]
 -- @param    term [description]
 -- @param    onlyMaxValue [description]
 -- @return    table
 -- 子类实现
function DdzSuitAnalyzer:doAnalysizeLaizi(cards,term,onlyMaxValue)
end

---
 -- [containsPossibleCards 提牌算法，大致确认下手牌中是否可以分析出对应可能符合牌型的牌出来 ]
 -- @DateTime 2017-11-08
 -- @param    cards [description]
 -- @param    term [description]
 -- @return    [description]
 --
function DdzSuitAnalyzer:containsPossibleCards(cards,term)
end

---
 -- [doListPossibleCards description]
 -- @DateTime 2017-11-08
 -- @param    cards [description]
 -- @param    term [description]
 -- @return    table
 -- 子类实现
function DdzSuitAnalyzer:doListPossibleCards(cards,term)
end

---
 -- [dtor 默认类的销毁方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function DdzSuitAnalyzer:dtor(...)
  
end


return DdzSuitAnalyzer;

